package net.gylka.newsreader;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class ViewModeDialogFragment extends DialogFragment {

	private int mViewModeSetting;
	private ViewModeChangeListener mViewModeChangeListener;
	
	public static ViewModeDialogFragment newInstance(int viewModeSetting) {
		ViewModeDialogFragment dialogFragment = new ViewModeDialogFragment();
		dialogFragment.mViewModeSetting = viewModeSetting;
		return dialogFragment;
	}
	
	@Override
	public void onAttach(Activity activity) {
		//TODO: Need exception handling if activity does not implement needed interface
		this.mViewModeChangeListener = (ViewModeChangeListener) activity;
		super.onAttach(activity);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.dialog_view_mode, container, false);
		this.getDialog().setTitle(R.string.dialog_view_mode_title);
		
		RadioGroup radioGroupViewMode = (RadioGroup) view.findViewById(R.id.radioGroupViewMode);
		if (mViewModeSetting == NewsReaderSettings.VIEW_MODE_HTML) { radioGroupViewMode.check(R.id.radioViewModeHtml); }
		if (mViewModeSetting == NewsReaderSettings.VIEW_MODE_PLAIN_TEXT) { radioGroupViewMode.check(R.id.radioViewModePlainText); }
		RadioButton radioViewModeHtml = (RadioButton) view.findViewById(R.id.radioViewModeHtml);
		radioViewModeHtml.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mViewModeChangeListener.onViewModeChanged(NewsReaderSettings.VIEW_MODE_HTML);
				dismiss();
			}
			
		});
		
		RadioButton radioViewModePlainText = (RadioButton) view.findViewById(R.id.radioViewModePlainText);
		radioViewModePlainText.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mViewModeChangeListener.onViewModeChanged(NewsReaderSettings.VIEW_MODE_PLAIN_TEXT);
				dismiss();
			}
			
		});
		
		return view;
	}

	
}
